﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TpWeb.Domain.BizObj;
using TpWeb.Domain.Repositories;

namespace TpWeb.Dal.Repositories
{
    internal class Repository<TEntity, TKey> : IRepository<TEntity, TKey>
        where TEntity : class, IEntity<TKey>
    {
        private DbContext _context;
        private DbSet<TEntity> _dbSet;
        
        public Repository(DbContext context, DbSet<TEntity> dbSet)
        {
            _context = context;
            _dbSet = dbSet;
        }

        public TEntity Find(TKey key)
        {
            throw new NotImplementedException();
        }

        public IQueryable<TEntity> All
        {
            get { return _context.Set<TEntity>(); }
        }

        public IQueryable<TEntity> AllIncluding(params System.Linq.Expressions.Expression<Func<TEntity, object>>[] includeProperties)
        {
            throw new NotImplementedException();
        }

        public virtual void Insert(TEntity ent)
        {
            _context.Set<TEntity>().Add(ent);
            _context.SaveChanges();

        }

        public void Update(TEntity ent)
        {
            _context.Set<TEntity>().AddOrUpdate(ent);
            _context.SaveChanges();
        }

        public void Delete(TEntity ent)
        {
            throw new NotImplementedException();
        }



        public int? Identity()
        {
            var qres = _context.Database.SqlQuery<int?>("select SCOPE_IDENTITY() as id");
            var res = qres.FirstOrDefault<int?>();
            return res;
        }
    }
}
